package com.hhu.wangzb.server.service.impl;

import com.hhu.wangzb.common.entity.PvDistrictEntity;
import com.hhu.wangzb.server.service.PvAreaService;
import com.hhu.wangzb.server.service.PvDistrictService;
import com.hhu.wangzb.server.service.RegionDetailService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.Query;

import com.hhu.wangzb.server.dao.ProjectDetailDao;
import com.hhu.wangzb.common.entity.ProjectDetailEntity;
import com.hhu.wangzb.server.service.ProjectDetailService;


@Service("projectDetailService")
public class ProjectDetailServiceImpl extends ServiceImpl<ProjectDetailDao, ProjectDetailEntity> implements ProjectDetailService {

    @Autowired
    PvDistrictService pvDistrictService;

    @Autowired
    RegionDetailService regionDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProjectDetailEntity> page = this.page(
                new Query<ProjectDetailEntity>().getPage(params),
                new QueryWrapper<ProjectDetailEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProjectDetailEntity> queryByParamsMap(Map<String, Object> paramsMap) {

        QueryWrapper<ProjectDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(paramsMap);

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        // 判断该项目下是否有用户，有则无法删除


        // 根据删除ids查询
        List<ProjectDetailEntity> projectDetailEntities = baseMapper.selectBatchIds(idList);
        // 子节点id
        List<Integer> pvaIds = new ArrayList<>();
        List<Integer> rgIds = new ArrayList<>();

        for (ProjectDetailEntity entity : projectDetailEntities) {
            pvaIds.add(entity.getPvaId());
            rgIds.add(entity.getRgId());
        }

        // 移除子节点
        if (pvaIds.size() != 0) pvDistrictService.removeByIds(pvaIds);
        if (rgIds.size() != 0) regionDetailService.removeByIds(rgIds);
        // 移除父节点
        return super.removeByIds(idList);
    }
}